<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * 商品分类
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$store_id=$this->store_id;
if(!$store_id){
	exi('请先选择门店！','error');
}
if($_RQ['table_id']){
		$table_id = intval($_RQ['table_id']);
		$this->session('table_id',$table_id);
}
if($_RQ['op']=='main'){
	$store=pdo_get('jy_weishop_store',array('id'=>$store_id));
	$groups = pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>0),array('id','title'));
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if($_RQ['pcate']){
		$pcate = intval($_RQ['pcate']);
		$params['pcate']=$pcate;
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$carts=$this->session('carts');
	$allnum =0;
	if($carts){
		foreach($carts as $k=>&$v){
			$allnum += $v['num'];
		}
	}else{
		$carts = array();
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 30;
	$total='';
	$fields=array('id','title','pcate','status','storeid','image','price','isoption');
	$list=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'id','id desc');
	if($list){
		foreach($list as &$g){
			$g['cartnum']= $carts[$g['id']]['num'] >0 ? $carts[$g['id']]['num'] : 0;
			$goodsprice += $g['cartnum']*$g['price'];
			//规格
			$specs_list=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$g['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
			//已选规格
			$selectspecs = array();
			$selespename = '';
			if($specs_list){
					foreach($specs_list as $k=>&$sp){
						$sp['items']=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$sp['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
						$selectspecs[$k]=$sp['items'][0]['id'];
						$selespename .=$sp['items'][0]['title'].'，';
					}
			}
			//默认已选参数：
			$spsids = '';
			foreach($selectspecs as $k=>&$s){
					if($k==0){
						$spsids .= $s;
					}else{
						$spsids .= '_'.$s;
					}
			}
			$g['options'] = pdo_get('jy_weishop_goods_option',array('goodsid'=>$g['id'],'specs'=>$spsids));
			//默认商品规格价格：
			if($return['options']){
				$g['price']=$g['options']['price'];
			}
			$g['selespename'] = $selespename;
			$option_list=pdo_getall('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$g['id']),array('id','specs','thumb','stock','price','productprice','costprice','goodssn','productsn','weight'),'','paixu asc');
			if($option_list){
					foreach($option_list as &$i){
						$i['specs']=explode('_',$i['specs']);
						foreach($i['specs'] as $j){
							$op=pdo_get('jy_weishop_goods_spec_item',array('id'=>intval($j)),array('specid','title'));
							$spec_title=pdo_getcolumn('jy_weishop_goods_spec',array('id'=>$op['specid']),'title');
							$i[$spec_title]=$op['title'];
						}
						$i['num']=$i['stock'];
						$i['old_price']=$i['productprice'];
						$i['cost_price']=$i['costprice'];
						$i['bm']=$i['goodssn'];
						$i['tm']=$i['productsn'];
						$i['image']=$i['thumb'];
					}
					$specs_list['option']=$option_list;
			}
			$g['specs_list'] = $specs_list;
		}
	}
	$allprice = $goodsprice - $youhui;
	$return['list']=$list;
	$return['groups']=$groups;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['params']['pcate'] =$params['pcate']?$params['pcate']:0;
	$return['allnum']=$allnum;
	$return['allprice']=$allprice;
	$return['orderprice']=$allprice;
	$return['in_pay']=$store['in_pay'];
	$return['order_type']=$this->session('order_type');
	$return['carts']=$carts;
	$return['post']=array('sendtype'=>2,'paytype'=>0,'is_pay'=>1);
	set_html('framess',$this->menustore());
	exi($return);
}
if($_RQ['op']=='diancai'){
	$post=get_postdata();
	$goodid=intval($post['goodid']);
	$num=intval($post['num']);
	$optionsid=intval($post['optionsid']);
	$carts=$this->session('carts');
	if(!$carts){
		$carts=array();
	}
	if($num < 1){
		unset($carts[$goodid]);
	}else{
		$carts[$goodid]['num']=$num;
		$goodinfo=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goodid));
		$carts[$goodid]['goodstitle'] = $goodinfo['title'];
		$carts[$goodid]['goodsprice'] = $goodinfo['price'];
		$packageinfo=json_decode($goodinfo['package'],true);
		$package=$carts[$goodid]['package'];
		if(!empty($packageinfo)){
			foreach($packageinfo as $k=>&$v){
				if($k == $packageid){
					$package[$k]['title']=$v['title'];
					$package[$k]['price']=$v['price']?$v['price']:$goodinfo['price'];
					if($_RQ['plus']){
						$package[$k]['num'] +=1;
					}else{
						$package[$k]['num'] -=1;
						if($package[$k]['num'] == 0){
							unset($package[$k]);
						}
					}
				}
			}
			$carts[$goodid]['package'] = $package;
		}
	}
	$carts=$this->session('carts',$carts);
	$return['carts'] = $this->session('carts');
	exi($return);
}
if($_RQ['op']=='get_optionsid'){
	$post=get_postdata();
	$id=$post['goodsid'];
	if(!$id){
		exi('请先选择商品！','error');
	}
	$return['options'] = pdo_get('jy_weishop_goods_option',array('goodsid'=>$id,'specs'=>$post['specid']));
	exi($return);
}
if($_RQ['op']=='unsetcarts'){
	$carts=$this->session('carts',array());
	$return['carts'] = $this->session('carts');
	exi($return);
}
if($_RQ['op']=='addorder'){
	$post=get_postdata();
	$carts=$post['carts'];
	//print_r($post);exit;
	$store_id=$this->session('store_id');
	if(!$store_id){
		exi('请先选择门店！','error','jy_weishop/index/');
	}
	$store=pdo_get('jy_weishop_store',array('id'=>$store_id));
	$youhui = 0;
	if(!$carts){
		exi('没有选择商品！','error');
	}
	$total=0;
	//处理订单
	if($carts){
		//从购物车购买
		foreach($carts as $k=>&$cart){
			$goods=pdo_get('jy_weishop_goods',array('id'=>$k),array('title','image','price','total','totalcnf','weight','active_plugin','active_id','storeids','merchid'));
			if(!$goods){
				exi('指定商品不存在！','error','goback');
			}
			$goods['goodsid']=$k;
			$goods['total']=$cart['num'];
			$goods['price']=$cart['price'];
			$this->model('goods')->check_buy($goods['id'],$cart['num']);
			if($cart['optionid']){
				$option_goods=pdo_get('jy_weishop_goods_option',array('id'=>$cart['optionid'],'uniacid'=>$_SESSION['uniacid'],'goodsid'=>$k),array('price','stock','specs'));
				if(!$option_goods){
					exi('指定商品不存在！','error','goback');
				}
				$specs = explode("_", $option_goods['specs']);
				$specsall = pdo_getall('jy_weishop_goods_spec_item',array('id'=>$specs,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
				if($specsall){
					$optiontitle='';
					foreach($specsall as $k=>&$sp){
						if($k==0){
							$optiontitle .=$sp['title'];
						}else{
							$optiontitle .='，'.$sp['title'];
						}
					}
				}
				$goods['optiondata']= $optiontitle;
			}
			//来自商户的分销
			$cart['merchid']=$cart['merchid']?$cart['merchid']:$goods['merchid'];
			$cart=array_merge($goods,$cart);
		}
		$order=array(
			'type'=>'cart',
			'list'=>$carts,
		);
	}
	//处理订单
	if(empty($order['list'])){
		exi('请先选择商品再下单！','error','goback');
	}
	foreach($order['list'] as &$goods){
		if($goods['active_plugin']){
			//商品在做活动，检查下库存是否不足
			$this->model('activity')->check_stock($goods['id'],$goods['active_plugin'],$goods['active_id'],$goods['total']);
			$active=$this->model('activity')->get_active($goods['id'],$goods['active_id'],$goods['active_plugin']);
			if($active['price']){
				$goods['price']=$active['price'];
				$goods['active']=$active;
			}
		}
	}
	//统计订单商品总数和总价
	$total=0;
	$total_price=0.00;
	$total_weight=0;
	foreach($order['list'] as $vc){
		$total+=$vc['total'];
		$total_price+=$vc['total']*$vc['price'];
		$total_weight+=$vc['weight']*$vc['total'];
	}
	
	$order['total']=$total;
	$order['total_price']=$total_price;
	$order['total_weight']=round($total_weight/1000,2);
	//计算运费
	if(!$order['sendtype'] || $order['sendtype']==1){
		$dispatch=pdo_get('jy_weishop_dispatch',array('uniacid'=>$_SESSION['uniacid'],'isdefault'=>1));
		if(!$dispatch){
			$dispatch=pdo_get('jy_weishop_dispatch',array('uniacid'=>$_SESSION['uniacid']));
		}
		if(!$dispatch){
			$order['dispatchprice']=0.00;
		}else{
			$order['dispatchprice']=$dispatch['showprice'];
			if($order['total_weight']>$dispatch['shouweight']){
				$order['dispatchprice']+=$dispatch['xuprice']*ceil(($order['total_weight']-$dispatch['shouweight'])/$dispatch['xuweight']);
			}
		}
	}
	
	//会员折扣价
	$order['level_de_price']=0.00;
	if(!$order['no_member_discount'] && $post['mid']){
		//允许使用会员折扣，整理会员折扣数据
		$member_levelid=pdo_getcolumn('jy_weishop_member',array('mid'=>$post['mid']),'level');
		if($member_levelid){
			$member_level=pdo_get('jy_weishop_member_level',array('id'=>$member_levelid));
			if($member_level){
				$member_level['discount']=$member_level['discount']?$member_level['discount']:10;
				$order['level_de_price']=$order['total_price']*($member_level['discount']/10);
			}
		}
	}
	//满减优惠价格：
	$sale_enough=$saleset['enough'];
	if($sale_enough){
		foreach($sale_enough as $k=>$ev){
			if($ev['man']>$order['total_price']){
				unset($sale_enough[$k]);
			}
		}
		paixu($sale_enough,'man','desc');
	}
	$order['sale_enough']=$sale_enough;
	$order['enough_de_price']=$sale_enough[0]['jian']?$sale_enough[0]['jian']:0.00;
	//优惠券优化价格：
	$order['coupon_de_price']=0.00;
	if(!$order['no_coupon_discount'] && $post['mid']){
		//整理优惠券数据，先获取用户所有待使用优惠券，再从中筛选出未过期的优惠券且本次下单可用的优惠券！
		$coupons=pdo_getall('jy_weishop_coupon_getlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$post['mid'],'isuse'=>0),array(),'id');
		if($coupons){
			foreach($coupons as $key=>&$coupon){
				$coupon_detail=pdo_get('jy_weishop_coupon',array('id'=>$coupon['couponid'],'uniacid'=>$_SESSION['uniacid']));
				if(!$coupon_detail){
					unset($coupons[$key]);
					continue;
				}
				if(!$coupon_detail['status']){
					//优惠券状态为不可用
					unset($coupons[$key]);
					continue;
				}
				//判断使用限制
				if($coupon_detail['limitusetype']){
					switch($coupon_detail['limitusetype']){
						case 1://不可与促销优惠同时使用
							
						case 2://不可与会员折扣同时使用
							if($order['level_de_price']){
								unset($coupons[$key]);
								continue;
							}
						case 3://不可与促销、折扣同时使用
							if($order['level_de_price']){
								unset($coupons[$key]);
								continue;
							}
					}
				}
				//判断有效期
				if($coupon_detail['timelimittype']==0){
					//有效期类型为领取后若干天内使用
					$days=(TIMESTAMP-$coupon['createtime'])/86400;
					if($coupon_detail['timedays']<$days){
						//已过期
						unset($coupons[$key]);
						continue;
					}
				}else{
					$coupon_detail['limittimes']=json_decode($coupon_detail['limittimes'],true);
					if(TIMESTAMP<$coupon_detail['limittimes']['start'] || TIMESTAMP>$coupon_detail['limittimes']['end']){
						//不在有效期内
						unset($coupons[$key]);
						continue;
					}
				}
				//判断是否当前可用
				if($coupon_detail['limitgoodtype'] && $coupon_detail['limitgoodids']){
					$coupon_detail['limitgoodids']=json_decode($coupon_detail['limitgoodids'],true);
					$check=false;
					foreach($order['list'] as $goods){
						if(in_array($goods['goodsid'],$coupon_detail['limitgoodids'])){
							if($goods['price']*$goods['total']>$coupon_detail['enough']){
								$check=true;
								switch($coupon_detail['coupontype']){
									case 0://立减
										$coupon['de_price']=$coupon_detail['deduct'];
										break;
									case 1://折扣
										$coupon['de_price']=(1-$coupon_detail['discount']/10)*$goods['price']*$goods['total'];
										break;
									default:
										$coupon['de_price']=0;
								}
							}
						}
					}
					if(!$check){
						//不可用
						unset($coupons[$key]);
						continue;
					}
				}
				if($coupon_detail['limitgoodcatetype'] && $coupon_detail['limitgoodcateids']){
					$coupon_detail['limitgoodcateids']=json_decode($coupon_detail['limitgoodcateids'],true);
					$check=false;
					foreach($order['list'] as $goods){
						if(in_array($goods['cateid'],$coupon_detail['limitgoodcateids'])){
							if($goods['price']*$goods['total']>$coupon_detail['enough']){
								$check=true;
								switch($coupon_detail['coupontype']){
									case 0://立减
										$coupon['de_price']=$coupon_detail['deduct'];
										$coupon['content']="满{$coupon_detail['enough']}减{$coupon_detail['deduct']}";
										break;
									case 1://折扣
										$coupon['de_price']=(1-$coupon_detail['discount']/10)*$goods['price']*$goods['total'];
										$coupon['content']="满{$coupon_detail['enough']}打{$coupon_detail['discount']}折";
										break;
									case 2://返利
										$coupon['de_price']=0;
										$coupon['content']="满{$coupon_detail['enough']}返{$coupon_detail['backmoney']}余额，{$coupon_detail['backcredit']}积分，{$coupon_detail['backredpack']}现金";
										break;
									default:
										$coupon['de_price']=0;
										$coupon['content']='';
								}
							}
						}
					}
					if(!$check){
						//不可用
						unset($coupons[$key]);
						continue;
					}
				}
				$coupon=array_merge($coupon_detail,$coupon);
				
			}
		}

		if(!empty($coupons)){
			//存在可用优惠券，选择一个抵扣金额最大的作为默认选中的优惠券
			foreach($coupons as $coupon){
				if(!$order['coupon_de_price'] || $order['coupon_de_price']<$coupon['de_price']){
					$order['coupon_de_price']=$coupon['de_price'];
					$item['couponid']=$coupon['id'];
				}
			}
		}
	}
	$_SESSION['coupons']=$return['coupons']=$coupons?$coupons:array();
	//整理收货地址信息
	if($post['mid']){
		$member_address=pdo_getall('core_address',array('mid'=>$post['mid']));
		if($member_address){
			foreach($member_address as &$address){
				$address['pcd']=json_decode($address['pcd'],true);
				if($address['isdefault']){
					$item['addressid']=$address['id'];
					$item['address']=$address;
					break;
				}
			}
			if(!$item['addressid']){
				$item['addressid']=$member_address['0']['id'];
				$item['address']=$member_address['0'];
			}
		}
	}
	
	//配送
	$order['deliverytime'] = $post['deliverytime']?$post['deliverytime']:date("m月d日",strtotime("+2 day"));
	//整理最终价
	$order['endprice']=price($order['total_price'] + $order['dispatchprice'] - $order['level_de_price']  - $order['coupon_de_price'] - $order['enough_de_price']);
	$order['no_dispatch_price']=price($order['total_price'] - $order['level_de_price']  - $order['coupon_de_price'] - $order['enough_de_price']);
	/*
		赠品
		先根据订单金额找赠品，再根据商品找赠品
	*/
	$goods_gifts=$this->pdo_getall('goods_gift',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'activity'=>0));
	$current_gift=array();
	if($goods_gifts){
		foreach($goods_gifts as $gift){
			if($gift['orderprice'] && $gift['orderprice']<$order['no_dispatch_price']){
				if(!$current_gift || $current_gift['orderprice']<$gift['orderprice']){
					$current_gift=$gift;
				}
			}
		}
	}
	if(!$current_gift){
		$goods_gifts=$this->pdo_getall('goods_gift',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'activity'=>1));
	}
	if($goods_gifts){
		foreach($goods_gifts as $gift){
			if($gift['goodsid']){
				foreach($order['goods'] as $goods){
					if($goods['goodsid']==$gift['goodsid']){
						$current_gift=$gift;
						break;
					}
				}
			}
			if($current_gift){
				break;
			}
		}
	}
	if($current_gift){
		$order['gift_id']=$current_gift['id'];
		$return['gifts']=json_decode($current_gift['gifts'],true);
	}
	$order['total_price']=price($order['total_price']);
	//提交订单
	//整理订单数据
	$coupons=$_SESSION['coupons'];unset($_SESSION['coupons']);
	//公共参数
	$new_order=array(
		'mid'=>$post['mid']?$post['mid']:0,
		'goodsprice'=>$order['total_price'],
		'remark'=>trim($post['remark']),
		'goods'=>$order['list'],
		'merchid'=>$order['merchid'],
	);
	$new_order['invoce']=array(
		'type'=>$post['invoicetype'],
		'goodstype'=>$post['invoicegoodstype'],
		'name'=>$post['invoicename'],
		'code'=>$post['invoicecode'],
		'email'=>$post['email'],
	);
	//会员折扣
	if(!$order['no_member_discount'] && $order['level_de_price']){
		$new_order['level_de_price']=$order['level_de_price'];
	}
	//满减
	$new_order['enough_de_price']=$order['enough_de_price'];
	//优惠券
	if(!$order['no_coupon_discount']){
		$couponid=intval($post['couponid']);
		if($couponid && $coupons[$couponid]){
			//使用优惠券
			$new_order['couponid']=$couponid;
			$new_order['coupon_de_price']=$coupons[$couponid]['de_price'];
		}
	}
	if($order['sendtype']){
		$new_order['sendtype']=$order['sendtype'];
	}else{
		$sendtype=intval($post['sendtype']);
		if(!$sendtype){
			exi('请先选择配送方式！','error');
		}
		$new_order['sendtype']=$sendtype;
	}
	$new_order['price']=$new_order['goodsprice']-$new_order['coupon_de_price']-$new_order['enough_de_price']-$new_order['level_de_price'];
	if($new_order['sendtype']==1){
		//快递配送
		$new_order['price']+=$order['dispatchprice'];
		$new_order['addressid']=intval($post['addressid']);
		if(!$new_order['addressid']){
			exi('请先选择收货地址！','error');
		}
		if($post['juli']){
			$merchid=pdo_getcolumn('jy_weishop_goods',array('id'=>$_SESSION['goodsid']),'merchid');
			$l=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid),array('lat','lng'));
			if($l['lng'] && $l['lat']){
				$d=cfc('tools')->getdistance($post['lat_lng']['lng'],$post['lat_lng']['lat'],$l['lng'],$l['lat']);
				if($d/1000>$post['juli']){
					exi('超出配送距离,无法购买','error','/goods.detail//id='.$_SESSION['goodsid']);
				}
			}
		}		
	}elseif($new_order['sendtype']==2){
		//门店自提
		$new_order['storeid']=$this->store_id;
		if(!$new_order['storeid']){
			exi('请先选择门店！','error');
		}
	}elseif($new_order['sendtype']==3){
		//虚拟发货
		
	}
	if($new_order['price']<=0){
		exi('订单金额不能为零！','error');
	}
	if($order['gift_id']){
		$new_order['gift_id']=$order['gift_id'];
	}
	$order_id=$this->model('order')->create($new_order,$post['mid']);
	//exi('~~~'.$post['is_pay'],'error');
	pdo_update('jy_weishop_order',array('status'=>1,'ispay'=>$post['is_pay']),array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
	$content1 .= "------------------\n";
	if($post['mid']){
		$mobile=pdo_getcolumn('core_members',array('id'=>$post['mid']),'mobile');	
		$nickname=pdo_getcolumn('core_members',array('id'=>$post['mid']),'nickname');	
	}else{
		$mobile=$post['mobile']?$post['mobile']:'无';
		$nickname=$post['username']?$post['username']:'店面客户';
	}
	$mobile=$post['mobile']?$post['mobile']:$mobile;
	$send_type=$this->send_types[$post['sendtype']];
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$selfcount=pdo_count('jy_weishop_order_self',array('uniacid'=>$_SESSION['uniacid'],'createtime >'=>$daytime),'id');
	$selfnumber=sprintf("%05d", $selfcount+1);
	$self=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$mid,
		'storeid'=>$this->store_id,
		'orderid'=>$order_id,
		'table_id'=>0,
		'username'=>$nickname,
		'mobile'=>$mobile,
		'address'=>$post['address'],
		'number'=>$selfnumber,
		'status'=>0,
		'type'=>0,
		'selftime'=>strtotime($post['deliverytime']),
		'createtime'=>time()
	);
	$this->pdo_insert('order_self',$self);
	$tpldata=array(
		'first'=>array('value'=>'订单下单通知','color'=>''),
		'tradeDateTime'=>array('value'=>'下单时间:'.date('Y-m-d h:i:s',TIMESTAMP),'color'=>'#ff510'),
		'orderType'=>array('value'=>'订单类型:'.$send_type,'color'=>'#ff510'),
		'customerInfo'=>array('value'=>'下单人'.$nickname,'color'=>'#ff510'),
		'orderItemName'=>array('value'=>'商品详情','color'=>'#ff510'),
		'orderItemData'=>array('value'=>'商品详情内容\n'.$content1,'color'=>'#ff510'),
		'remark'=>array('value'=>'其他信息','color'=>'#ff510'),
	);
	$url=custom_url('jy_weishop/order.list/',true);
	$text = '';
	if($order['send_type'] == 2){
		$text .='取货号:'.$selfnumber."\n";
	}
	$is_pays=array('未支付','已支付');
	$text .='下单时间:'.date('Y-m-d h:i:s',TIMESTAMP)."\n";
	$text .='订单类型:'.$send_type."\n";
	$text .="是否支付:".$is_pays[$post['is_pay']]."\n";
	$text .="下单人:代客下单\n";
	$text .="【菜品详情】\n";
	$text .="------------------\n";
	$text .=$content1."\n";
	$text .='用户名:'.$nickname."\n";
	$text .='手机号码:'.$mobile."\n";
	//发信息
	//$this->notice('order_new',$text,$tpldata,$post['mid'],$url,'#980000');
	//打印
	$this->model('printmsg')->storeprint($text,$order_id,0,1);
	if($_RQ['isreturn']){
		exi('success');
	}else{
		exi('提交成功');
	}
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择桌号！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['status'])){
		$update['status']=intval($post['status']);
	}
	if($update){
		pdo_update('jy_weishop_table',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_table',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回桌号类型列表'),
		),
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'string','title'=>'桌号编号','name'=>'title','help'=>''),
					array('type'=>'select','title'=>'桌号类型','name'=>'group_id','help'=>'','options'=>'groups'),
					array('type'=>'image','title'=>'桌号图片','name'=>'image','help'=>''),
					array('type'=>'switch','title'=>'是否启用','name'=>'status','help'=>''),
				)
			),
		)
	);
	$return['groups']=pdo_getall('jy_weishop_table_group',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id),array('id','title'),'id');
	$return['htmls']=$htmls;
	set_html('framess',$this->menustore());
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'group_id'=>intval($postdata['group_id']),
		'status'=>intval($postdata['status']),
		'image'=>trim($postdata['image']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_weishop_table',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['store_id']=$store_id;
		pdo_insert('jy_weishop_table',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_weishop_table',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}